//
//  BannerViewController.swift
//  cabbage_swift
//
//  Created by dfsx6 on 2022/3/24.
//

import UIKit

class BannerViewController: UIViewController {

 
    fileprivate var collectionView: UICollectionView!
    
    
}

extension BannerViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = .white
        
        let layout = BannerCollectionViewLayout.init()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: view.bounds.width/2, height: 150)
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.frame = CGRect.init(x: 0, y: 200, width: view.bounds.width, height: 200)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .red
        collectionView.register(BannerCollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
        view.addSubview(collectionView)
    }

}

extension BannerViewController: UICollectionViewDelegate {
    
}


extension BannerViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 7
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! BannerCollectionViewCell
        cell.lable.text = "\(indexPath.row)"
        cell.backgroundColor = .green
        return cell
    }
}

extension BannerViewController {
    
}

class BannerCollectionViewCell: UICollectionViewCell {
    
    let lable = UILabel.init()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        lable.textAlignment = .center
        contentView.addSubview(lable)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        lable.frame = contentView.bounds
    }
}

class BannerCollectionViewLayout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        guard let collectionView = collectionView else {
            return
        }
        self.minimumInteritemSpacing = 0.0
        self.minimumLineSpacing = 0.0
        let margin = (collectionView.bounds.size.width - itemSize.width)/2
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: margin, bottom: 0, right: margin)
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let elements = super.layoutAttributesForElements(in: rect)
        elements?.forEach({ att in
            let centerx = self.collectionView!.center.x
            let step = abs(centerx - (att.center.x - self.collectionView!.contentOffset.x))
            let scale = fabsf(cosf(Float(step/centerx * Double.pi)/5))
            
            print("xxxxx3", scale)
            
            att.transform = CGAffineTransform.init(scaleX: CGFloat(scale), y: CGFloat(scale))

        })
        
        
       
        return elements
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        var pOffset = proposedContentOffset
        let rect = CGRect.init(origin: CGPoint.init(x: pOffset.x, y: 0), size: self.collectionView!.bounds.size)
        var elements = [UICollectionViewLayoutAttributes]()
        elements.append(contentsOf: self.layoutAttributesForElements(in: rect) ?? [])
        let centerx = pOffset.x + self.collectionView!.bounds.width/2
        var minDelta: CGFloat = 0
        elements.forEach({ att in
            if (abs(minDelta) > abs(att.center.x - centerx)) {
                minDelta = att.center.x - centerx
            }
        })
        pOffset.x += minDelta
        
//        print("xxxxx1", proposedContentOffset, pOffset, velocity)

        return pOffset
    }
}
